<div class="container">
  <div class="bg-putih">

    <div class="jdl-page">
      <?php echo $judul ?>
      <div class="clearfix"></div>
    </div>
    <?php
      $KdKelompok = $this->session->userdata('kelompok');
      $KdObat     = $this->session->userdata('obat');
      $id_petugas = $this->session->userdata('petugas');
    ?>
    <div class="row">
      <div class="col-md-2">
        <div class="btn-group btn-group-sm" role="group">
          <a href="<?php echo site_url('transaksi_penjualan/excel') ?>" class="btn btn-warning"><i class="fa fa-file-excel-o"></i> Unduh Excel</a>
        </div>
      </div>
      <div class="col-xs-10" style="float:right">
        <div class="input-group-3">
          <?php echo form_open('transaksi_penjualan/cari'); ?>
          <button id="btn-search" type="submit" class="btn btn-primary btn-sm"><span class="glyphicon glyphicon-search"></span></button>
          <select id="selector" class="form-control input-sm" name="obat" style="width:20%">
            <option value=""> - - Pilih Obat - - </option>     
            <?php
              foreach ($obat as $value) {
                if($value['KdObat'] == $KdObat){
                  $s = "selected";
                }else{
                  $s = "";
                }
                echo "<option value='".$value['KdObat']."' $s>".$value['NmObat']."</option>";
              }
            ?>         
          </select>   
          <span id="frm-input">
            <select name="kelompok" class="form-control input-sm" id="form-no-radius" style="width:19%">  
              <option value=""> - - Pilih Kelompok - - </option>  
              <?php
                foreach ($kelompok as $value) {
                  if($value['KdKelompok'] == $KdKelompok){
                    $s = "selected";
                  }else{
                    $s = "";
                  }
                  echo "<option value='".$value['KdKelompok']."' $s>".$value['NmKelompok']."</option>";
                }
              ?>
            </select>
            <select name="petugas" class="form-control input-sm form-no-radius" style="width:19%">
              <option value=""> - - Pilih Petugas - - </option>
              <?php
                foreach ($petugas as $value) {
                  if($value['id'] == $id_petugas){
                    $s = "selected";
                  }else{
                    $s = "";
                  }
                  echo "<option value='".$value['id']."' $s>".$value['nickname']."</option>";
                }
              ?>  
            </select>
            <input id="after" type="text" name="tgl_akhir" class="form-control input-sm" placeholder="Tanggal Akhir" style="width:19%" value="<?php echo $this->session->userdata('tgl_akhir') ?>"/>
            <input id="before" type="text" name="tgl_awal" class="form-control input-sm" placeholder="Tanggal Awal" style="width:19%" value="<?php echo $this->session->userdata('tgl_awal') ?>"/>
          </span>
          <?php echo form_close(); ?> 
        </div>
      </div>
    </div>
    <table class="table table-hover margintop20">
      <thead>
        <tr>
          <th width="5%">No</th>
          <th width="10%">Kode</th>
          <th width="10%">Tanggal</th>
          <th width="15%">User</th>
          <th width="20%">Keterangan</th>
          <th width="35%" colspan="4">Transaksi</th>
          <!-- <th width="5%"></th> -->
        </tr>
      </thead>
      <tbody>
        <?php 
          foreach($results as $row):
            if($KdKelompok != '' || $KdObat != ''){
              $detail   = get_detail_jual_by($row['KdJual'],$KdKelompok,$KdObat);
            }else{
              $detail   = get_detail_jual($row['KdJual']);
            }
            $jumlah   = count($detail);
            $rowspan  = $jumlah + 1;
            ?>
            <tr>
              <td rowspan="<?php echo $rowspan ?>"><?php echo $ordinal_num++; ?></td>
              <td align="center" rowspan="<?php echo $rowspan ?>"><?php echo $row['KdJual'] ?></td>
              <td rowspan="<?php echo $rowspan ?>"><?php echo id_date($row['Tanggal'],'short'); ?></td>
              <td rowspan="<?php echo $rowspan ?>"><?php echo $row['nickname'] ?></td>
              <td rowspan="<?php echo $rowspan ?>"><?php echo $row['Keterangan'] ?></td>
              <td>
                <?php 
                  if($jumlah > 0){
                    echo $detail[0]['NmObat'];
                  }
                ?>
              </td>  
              <td align="center">
                <?php 
                  if($jumlah > 0){
                    echo formatAngka($detail[0]['Jumlah']);
                  }
                ?>
              </td>  
              <td align="right">
                <?php 
                  if($jumlah > 0){
                    echo formatAngka($detail[0]['Harga']);
                  }
                ?>
              </td>  
              <td align="right">
                <?php 
                  if($jumlah > 0){
                    echo formatAngka($total = $detail[0]['Jumlah'] * $detail[0]['Harga']); 
                  }
                ?>
              </td> 
              <!-- <td rowspan="<?php echo $rowspan ?>" align="center">
                <a href="<?php echo site_url("transaksi_penjualan/detail/$row[KdJual]") ?>" class="btn btn-xs btn-primary">Detail</a>
              </td> -->
            </tr>
            <?php
              if($jumlah > 0){
                $jml_item = $detail[0]['Jumlah'];
                $subtotal = $total;
                if($jumlah > 1){
                  for($i = 1; $i < $jumlah; $i++) {
                    ?>
                      <tr>
                        <td><?php echo $detail[$i]['NmObat'] ?></td>  
                        <td align="center"><?php echo formatAngka($detail[$i]['Jumlah']) ?></td>  
                        <td align="right"><?php echo formatAngka($detail[$i]['Harga']) ?></td>  
                        <td align="right"><?php echo formatAngka($total = $detail[$i]['Jumlah'] * $detail[$i]['Harga']); ?></td>
                      </tr>
                    <?php
                    $jml_item = $jml_item + $detail[$i]['Jumlah'];
                    $subtotal = $subtotal + $total;
                  }
                }
              }
              if($jumlah > 0){
            ?>
              <tr style="font-weight: bold;">
                <td>Subtotal</td>  
                <td align="center"><?php echo formatAngka($jml_item); ?></td>  
                <td>&nbsp;</td>  
                <td align="right"><?php echo formatAngka($subtotal); ?></td>
              </tr>
            <?php
              }
            ?>
        <?php endforeach; ?>
        <?php if(count($results) == 0): ?>
          <tr>
            <td colspan="10" align="center">Tidak ada data pembelian</td>
          </tr>
        <?php endif; ?>
      </tbody>
    </table>
    <center style="margin-top:-30px;">
      <ul class="pagination">
        <?php echo $pagination; ?>
      </ul>
    </center>
  </div>
</div>

<script type="text/javascript">
  $(document).ready(function(){
    $('#after, #before').datepicker({
      dateFormat: "dd/mm/yy",
      changeMonth: true,
      changeYear: true
    });
  });
</script>